package star.planets.meshview;



import javax.swing.*;


import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;
import java.util.Iterator;

import star.core.util.math.HTransform;
import star.crust.cartspace.framework.CrtBody;
import star.crust.cartspace.framework.CrtBodyGroup;
import star.crust.cartspace.framework.CrtBodyIterator;

public class J3DPanelCommand extends JPanel {
	private static final long serialVersionUID = 1L;
	private J3DSceneCanvas scene3D = null;
	private int nm = 4; 	// number of models

	private JLabel modelLabel[] = new JLabel[nm];
	private JTextField modelTextField[] = new JTextField[nm];
	private JButton loadButton[] = new JButton[nm]; 
	private JButton unloadButton[] = new JButton[nm]; 

	private JLabel txLabel[] = new JLabel[nm];
	private JTextField txTextField[] = new JTextField[nm];
	private JLabel tyLabel[] = new JLabel[nm];
	private JTextField tyTextField[] = new JTextField[nm];
	private JLabel tzLabel[] = new JLabel[nm];
	private JTextField tzTextField[] = new JTextField[nm];

	private JLabel rxLabel[] = new JLabel[nm];
	private JTextField rxTextField[] = new JTextField[nm];
	private JLabel ryLabel[] = new JLabel[nm];
	private JTextField ryTextField[] = new JTextField[nm];
	private JLabel rzLabel[] = new JLabel[nm];
	private JTextField rzTextField[] = new JTextField[nm];
	private JButton moveButton[] = new JButton[nm]; 
	
	private Hashtable<String, CrtBodyGroup> models = new Hashtable<String, CrtBodyGroup>();
	
	
	int i = 0;

	public J3DPanelCommand(J3DSceneCanvas scene3D) {
		super();
		
		this.scene3D = scene3D;
		
		setPreferredSize(new Dimension(200, 900));
		setMinimumSize(new Dimension(200, 900));

		setBounds(new Rectangle(0, 0, 180, 30));
		setBorder(BorderFactory.createRaisedBevelBorder());
		setLayout(null);

		try {
			int offset = 220;
			for(i=0; i < nm; i++) {
				modelLabel[i]= new JLabel();
				modelLabel[i].setText("M"+ (i+1));
				modelLabel[i].setBounds(new Rectangle(10, 10+offset*i, 150, 20));
				this.add(modelLabel[i], null);

				modelTextField[i] = new JTextField();
				modelTextField[i].setText("");
				modelTextField[i].setBounds(new Rectangle(40, 10+offset*i, 150, 20));
				modelTextField[i].setHorizontalAlignment(SwingConstants.LEFT);
				modelTextField[i].setBackground(Color.white);
				modelTextField[i].setBorder(BorderFactory.createRaisedBevelBorder());
				modelTextField[i].setEditable(true);
				this.add(modelTextField[i], null);

				loadButton[i] = new JButton();
				loadButton[i].setBounds(new Rectangle(10, 40+offset*i, 80, 30));
				loadButton[i].setText("LOAD");
				loadButton[i].addActionListener(new java.awt.event.ActionListener() {
					int id = i;
					public void actionPerformed(ActionEvent e) {
						loadButton_actionPerformed(e, id);
					}
				});
				this.add(loadButton[i], null);

				unloadButton[i] = new JButton();
				unloadButton[i].setBounds(new Rectangle(100, 40+offset*i, 90, 30));
				unloadButton[i].setText("UNLOAD");
				unloadButton[i].addActionListener(new java.awt.event.ActionListener() {
					int id = i;
					public void actionPerformed(ActionEvent e) {
						unloadButton_actionPerformed(e, id);
					}
				});
				this.add(unloadButton[i], null);

				// TX position 
				txLabel[i]= new JLabel();
				txLabel[i].setText("TX");
				txLabel[i].setBounds(new Rectangle(20, 80+offset*i, 30, 20));
				this.add(txLabel[i], null);

				txTextField[i] = new JTextField();
				txTextField[i].setText("20");
				txTextField[i].setBounds(new Rectangle(10, 100+offset*i, 50, 20));
				txTextField[i].setHorizontalAlignment(SwingConstants.RIGHT);
				txTextField[i].setBackground(Color.white);
				txTextField[i].setBorder(BorderFactory.createRaisedBevelBorder());
				txTextField[i].setEditable(true);
				this.add(txTextField[i], null);

				// TY position 
				tyLabel[i]= new JLabel();
				tyLabel[i].setText("TY");
				tyLabel[i].setBounds(new Rectangle(80, 80+offset*i, 30, 20));
				this.add(tyLabel[i], null);

				tyTextField[i] = new JTextField();
				tyTextField[i].setText("0");
				tyTextField[i].setBounds(new Rectangle(70, 100+offset*i, 50, 20));
				tyTextField[i].setHorizontalAlignment(SwingConstants.RIGHT);
				tyTextField[i].setBackground(Color.white);
				tyTextField[i].setBorder(BorderFactory.createRaisedBevelBorder());
				tyTextField[i].setEditable(true);
				this.add(tyTextField[i], null);

				// TZ position 
				tzLabel[i]= new JLabel();
				tzLabel[i].setText("TZ");
				tzLabel[i].setBounds(new Rectangle(140, 80+offset*i, 30, 20));
				this.add(tzLabel[i], null);

				tzTextField[i] = new JTextField();
				tzTextField[i].setText("0");
				tzTextField[i].setBounds(new Rectangle(130, 100+offset*i, 50, 20));
				tzTextField[i].setHorizontalAlignment(SwingConstants.RIGHT);
				tzTextField[i].setBackground(Color.white);
				tzTextField[i].setBorder(BorderFactory.createRaisedBevelBorder());
				tzTextField[i].setEditable(true);
				this.add(tzTextField[i], null);

				// RX position 
				rxLabel[i]= new JLabel();
				rxLabel[i].setText("RX");
				rxLabel[i].setBounds(new Rectangle(20, 120+offset*i, 30, 20));
				this.add(rxLabel[i], null);

				rxTextField[i] = new JTextField();
				rxTextField[i].setText("0");
				rxTextField[i].setBounds(new Rectangle(10, 140+offset*i, 50, 20));
				rxTextField[i].setHorizontalAlignment(SwingConstants.RIGHT);
				rxTextField[i].setBackground(Color.white);
				rxTextField[i].setBorder(BorderFactory.createRaisedBevelBorder());
				rxTextField[i].setEditable(true);
				this.add(rxTextField[i], null);

				// RY position 
				ryLabel[i]= new JLabel();
				ryLabel[i].setText("RY");
				ryLabel[i].setBounds(new Rectangle(80, 120+offset*i, 30, 20));
				this.add(ryLabel[i], null);

				ryTextField[i] = new JTextField();
				ryTextField[i].setText("0");
				ryTextField[i].setBounds(new Rectangle(70, 140+offset*i, 50, 20));
				ryTextField[i].setHorizontalAlignment(SwingConstants.RIGHT);
				ryTextField[i].setBackground(Color.white);
				ryTextField[i].setBorder(BorderFactory.createRaisedBevelBorder());
				ryTextField[i].setEditable(true);
				this.add(ryTextField[i], null);

				// RZ position 
				rzLabel[i] = new JLabel();
				rzLabel[i].setText("RZ");
				rzLabel[i].setBounds(new Rectangle(140, 120+offset*i, 30, 20));
				this.add(rzLabel[i], null);

				rzTextField[i] = new JTextField();
				rzTextField[i].setText("0");
				rzTextField[i].setBounds(new Rectangle(130, 140+offset*i, 50, 20));
				rzTextField[i].setHorizontalAlignment(SwingConstants.RIGHT);
				rzTextField[i].setBackground(Color.white);
				rzTextField[i].setBorder(BorderFactory.createRaisedBevelBorder());
				rzTextField[i].setEditable(true);
				this.add(rzTextField[i], null);

				moveButton[i] = new JButton();
				moveButton[i].setBounds(new Rectangle(50, 170+offset*i, 100, 30));
				moveButton[i].setText("MOVE");
				moveButton[i].addActionListener(new java.awt.event.ActionListener() {
					int id = i;
					public void actionPerformed(ActionEvent e) {
						moveButton_actionPerformed(e, id);
					}
				});
				this.add(moveButton[i], null);
				
			}
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}

	void loadButton_actionPerformed(ActionEvent e, int id) {	
		String fileName = "./res/"+ modelTextField[id].getText() + ".dae";
		CrtBodyGroup rootBodyGroup = scene3D.loadModel(fileName);
		if(rootBodyGroup != null)
			models.put(""+id, rootBodyGroup);
	}
	void unloadButton_actionPerformed(ActionEvent e, int id) {
		System.out.println("NOT IMPLEMENTED: unloadButton_actionPerformed() ID = " + id);
	}
	void moveButton_actionPerformed(ActionEvent e, int id) {
		CrtBodyGroup model = models.get(""+id);
		
		if(model == null)
			return;

		Iterator<CrtBody> bodyIterator = new CrtBodyIterator(model); 
		CrtBody body = null;
		HTransform transform = null;
		while(bodyIterator.hasNext()) {
			body = bodyIterator.next();
			transform = body.getAbsolutePosition();
			transform.translateX(Float.parseFloat(txTextField[id].getText()));
			transform.translateY(Float.parseFloat(tyTextField[id].getText()));
			transform.translateZ(Float.parseFloat(tzTextField[id].getText()));

			transform.rotateX((float) Math.toRadians(Float.parseFloat(rxTextField[id].getText())));
			transform.rotateY((float) Math.toRadians(Float.parseFloat(ryTextField[id].getText())));
			transform.rotateZ((float) Math.toRadians(Float.parseFloat(rzTextField[id].getText())));
			body.notifyPositionChanged();
		}
	}
}
